package com.itpan.Utils;

import com.itpan.Entity.Employee;
import com.itpan.common.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@Service
public class ImageUtils {


    @Value("${itpan.basePath}")
    private String basePath;
    public String saveAndgetNewName(MultipartFile file){



        try {
            Path path = Paths.get(basePath);

            // 如果目录不存在，创建目录
            if (!Files.exists(path)) {
                Files.createDirectories(path);
            }

            String originalFilename = file.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString() + suffix;
            Path newFilePath = path.resolve(newFileName);

            // 保存文件
            file.transferTo(newFilePath.toFile());

           return "http://localhost:8089/img/"+newFileName;
        } catch (IOException e) {
            throw new CustomException("图片上传失败");
        }
    }

    }

